/* ========================================================================= */
/* ------------------------------------------------------------------------- */
/*!
   \file			ETI_CplxTxt.h
   \date			september 2011
   \author		TNick

   \brief		Contains the definition for ETI_CplxTxt class


   Copyright (c) 2011, Nicu Tofan
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions are met:
   * Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
   * Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
   * Neither the name of the copyright holder nor the
   names of its contributors may be used to endorse or promote products
   derived from this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS" AND
   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
   WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   DISCLAIMED. IN NO EVENT SHALL NICU TOFAN BE LIABLE FOR ANY
   DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   */
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
#ifndef __ETI_CPLXTXT_INC__
#define __ETI_CPLXTXT_INC__
//
//
//
//
/*  INCLUDES    ------------------------------------------------------------ */

class		ETSubItem;
class		ETSheet;


#include	<QDebug>
#include    <QGraphicsObject>
#include	<QFont>
#include	<QDomElement>

#include    <generic/cc_l2d.h>

#include	<sheet/subitems/ETSI_Root.h>

/*  INCLUDES    ============================================================ */
//
//
//
//
/*  CLASS    --------------------------------------------------------------- */

/// class representing a complex text; this may or may not represent an equation
/**
*
*	As it is parented in ETSheet, the item inherits the
*	<a href="http://doc.qt.nokia.com/latest/qgraphicsitem.html">QGraphicsItem</a>
*	and gets most of the functionality from it.
*
*	This is a host for a row of ETSubItem elements. Having a flexible model,
*	the component items may have sub-items on their turn.
*
*
*	\section creating Creating new complex texts
*	To place a new complex text in a ETSheet the constructor needs to be provided
*	with the (valid) sheet pointer and a position inside that sheet (document).
*	Then, a default ETSI_SText will be created as the one and only element.
*	Please note that a ETI_CplxTxt shall never be completly empty, as this would
*	result in it being invisible to the user.
*
*	The external world creates new complex texts most often using
*	ETVpCmd_NewTopElem command.
*
*	\section dimensions Dimensions. Bounding box.
*	The class instance keeps track of it's size using a ETSI_Root element.
*	To tigger a computation of sizes for all contained sub-items, call
*	recompute(). It will either update the properties of all items or only for those
*	that are affected by changes.
*
*
*	\section save_load	Saving and loading from xml file
*	Dealing with file storage from this class's point of view is fairly easy.
*	it implements the usual loadXML() and saveXML() that make use of
*	convenience functions ETSheet::loadQICommon() and ETSheet::saveQICommon().
*	to save QGraphicsItem's properties then the task of loading / saving is
*	passed to embedded ETSI_Root sub-item.
*
*
*	\section various	Various
*	makeCurrent() function will select the root element embedded in this item
*	as the current sub-item in the provided viewport.
*
*/
class ETI_CplxTxt : public QGraphicsItem DDHC_INHERIT		{
	DDHC_CLASS

	//
	//
	//
	//
	/*  DEFINITIONS    ----------------------------------------------------- */

public:
	enum { Type = QGraphicsItem::UserType + 2 };


	/*  DEFINITIONS    ===================================================== */
	//
	//
	//
	//
	/*  DATA    ------------------------------------------------------------ */

private:

	/// the root item
	ETSI_Root		it_r;

	/*  DATA    ============================================================ */
	//
	//
	//
	//
	/*  FUNCTIONS    ------------------------------------------------------- */

private:

	/// internal function that is used by constructors to make basic initialisation
	void				init				( void );


	/// internal constructor used by loadXML() to create the new instance
	ETI_CplxTxt			();



public:

	/// constructor
	ETI_CplxTxt			(
			ETSheet *		sht,
			qreal			loc_x,
			qreal			loc_y);


	/// destructor
	virtual				~ETI_CplxTxt		( void );



	/// recompute internal data
	void				recompute		(bool b_full = false);


	/// make it current in specified viewport
	void				makeCurrent		(ETSheetView * vp)
	{ it_r.makeCurrent(vp); }


	/// recives a point and acts as response of a mouse click
	/**
	*	This function is used both by internal mechanism for click response
	*	and by any external parties that may be interested.
	*/
	void				performClick	(ETSheetView * p_vp, QPointF pt);



	/// expose root sub-item
	inline ETSI_Root *	rootSI			( void )
	{ return &it_r; }

	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name QGraphicsItem
 *	Reimplement some of the functions
 */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// the type of this item
	int					type			( void ) const
	{ return Type; }


	/// defines the outer bounds of the item as a rectangle
	QRectF              boundingRect    ( void ) const;


	/// paints the contents of an item in local coordinates
	void                paint           (
			QPainter *			painter,
			const QStyleOptionGraphicsItem *option,
			QWidget *			widget
			);


	/// gives the shape of this item
	QPainterPath		shape			( void ) const;


	/// tell if a point is inside this item
	bool				contains		(
			const QPointF&	point
			) const;



	bool				sceneEvent		(
			QEvent *		event);



	void	mousePressEvent			( QGraphicsSceneMouseEvent *event);
	void	mouseMoveEvent			( QGraphicsSceneMouseEvent *event);
	void	mouseReleaseEvent		( QGraphicsSceneMouseEvent *event);
	void 	mouseDoubleClickEvent	( QGraphicsSceneMouseEvent * event );
	void	contextMenuEvent		( QGraphicsSceneContextMenuEvent *event);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Internal root item
 *	Expose some of the characteristics of internal ETSI_Root item
 */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

	/// get the number of items
	inline int			count	( void ) const
	{ return it_r.count(); }

	/// get nth item
	inline ETSubItem*	item	(int i) const
	{ return it_r.item(i); }


	/// get first item
	inline ETSubItem*	first	( void )  const
	{ return it_r.first(); }


	/// get last item
	inline ETSubItem*	last	( void )  const
	{ return it_r.last(); }


	inline void			dbgDump	( void ) const
	{ it_r.dumpDbg2(); }

	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Others    */
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */


	/// called to determine the sub-item that was hit
	void		hitTest		(ETSubItem::HtData * h_data);


	/// the item is cleared and brought to a state just like after creation
	/**
 *	Specifically, all sub-items are removed. A new ETSI_SText item is
 *	created in default state and placed under root.
 */
	void		reinit		(
			bool			b_recomp = true
			);


	/// removes the item from the ETI_CplxTxt item; may tigger an update
	/**
 *	If multiple sub-items belonging to the
 *	same ETI_CplxTxt item have to be changed, \b b_recomp parameter may be
 *	set to false to prevent the function from recomputing all sub-items
 *	involved. The caller is responsible for calling ETI_CplxTxt::recompute()
 *	when all changes were applied.
 */
	void				remove			(
			bool			b_recomp = true
			);


	/// creates a new item containing specified type of sub-item
	static ETI_CplxTxt *	createHost	(ETSheet * dest, QPointF pos, ETSubItem::SIType sit_ty);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */



	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */
	/** @name Save and load	*/
	//@{
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

public:

	/// creates an viewport in specified sheet based on DOM node
	static ETI_CplxTxt *	loadXML			(
			ETSheet *	dest,
			QDomElement	dom_e
			);

	/// saves the item in provided DOM element
	void					saveXML			(
			QDomElement dom_e
			);


	//@}
	/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */




	/*  FUNCTIONS    ======================================================= */
	//
	//
	//
	//

};	/*	class ETI_CplxTxt	*/

/*  CLASS    =============================================================== */
//
//
//
//
#endif // __ETI_CPLXTXT_INC__
/* ------------------------------------------------------------------------- */
/* ========================================================================= */
